package controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import dao.LogDao;
import dao.UsersDao;
import util.MybatisSessionFactory;

@Controller
public class User {

	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	@ResponseBody
	public String register(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			@RequestParam(value = "password2", defaultValue = "") String password2,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		LogDao dao1 = Session.getMapper(LogDao.class);
		Map<String, Object> result = dao.queryone(account);
		if ("1".equals(result.get("counter").toString())) {
			return "ERR";
		}
		dao.add(account, password, "2078-09-09");
		Map<String, Object> result2 = dao.lastid();
		dao1.add2(result2.get("id").toString(), "注册", "0");
		Session.commit();
		return "OK";

	}

	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,

			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		LogDao dao1 = Session.getMapper(LogDao.class);
		Map<String, Object> result = dao.queryone(account);

		if ("0".equals(result.get("counter").toString())) {
			return "ERR";
		}
		response.addCookie(new Cookie("result", result.get("id").toString()));
		dao1.add2(result.get("id").toString(), "登陆", "0");
		Session.commit();
		// dao.add(account,password,"2078-09-09");
		// Session.commit();
		return "OK";

	}

	@RequestMapping(value = "/query_nickname.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query_(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") String id) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		Map<String, Object> result = dao.query_nickname(resultId);
		return result;
	}

	@RequestMapping(value = "/query_income.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_income(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map> result = dao.query_income(resultId);
		return result;
	}

	@RequestMapping(value = "/query_pay.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_pay(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map> result = dao.query_pay(resultId);
		return result;
	}

	@RequestMapping(value = "/query_in.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query_in(

	@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		Map<String, Object> result = dao.query_in(resultId);
		return result;
	}

	@RequestMapping(value = "/query_out.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query_out(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		Map<String, Object> result = dao.query_out(resultId);
		return result;
	}

	@RequestMapping(value = "/query_c.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_c(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map> result = dao.query_c(resultId);
		return result;
	}

	@RequestMapping(value = "/query_v.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_v(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map> result = dao.query_v(resultId);
		return result;
	}

	@RequestMapping(value = "/adda.action", method = RequestMethod.POST)
	@ResponseBody
	public String adda(

			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		System.out.println(date_time);
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		LogDao dao1 = Session.getMapper(LogDao.class);
		UsersDao dao = Session.getMapper(UsersDao.class);
		dao.adda(resultId, date_time, place, money_in, money_out, kind, remark);

		Map<String, Object> result2 = dao.lastid();
		dao1.add2(resultId, "支出入", result2.get("id").toString());
		Session.commit();
		return "OK";
	}

	@RequestMapping(value = "/query_p.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> query_p(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		Map<String, Object> result = dao.query_p(resultId);
		return result;
	}

	@RequestMapping(value = "/query_o.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_o(

	@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map> result = dao.query_o(resultId);
		return result;
	}

	@RequestMapping(value = "/query_u.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_u(

	@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		List<Map> result = dao.query_u(resultId);
		return result;
	}

	@RequestMapping(value = "/select.action", method = RequestMethod.POST)
	@ResponseBody
	public List<Map> select(
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		System.out.println(date_time + place + money_in + money_out + kind
				+ remark);

		SqlSession Session = MybatisSessionFactory.getSqlSession();
		//
		UsersDao dao = Session.getMapper(UsersDao.class);
		//
		List<Map> result = dao.select(resultId, date_time, place, money_in,
				money_out, kind, remark);

		Session.commit();
		return result;
	}

	@RequestMapping(value = "/query_null.action", method = RequestMethod.POST)
	@ResponseBody
	public String query_null(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		UsersDao dao = Session.getMapper(UsersDao.class);
		LogDao dao1 = Session.getMapper(LogDao.class);
		response.addCookie(new Cookie("result", null));
		dao1.add2(resultId, "退出", "0");
		Session.commit();

		return "OK";

	}

	@RequestMapping(value = "/querytwo.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> querytwo(
			@RequestParam(value = "id", defaultValue = "") String id,
			@CookieValue(value = "result", defaultValue = "") String resultId,

			HttpServletResponse response) {

		SqlSession Session = MybatisSessionFactory.getSqlSession();
		//
		UsersDao dao = Session.getMapper(UsersDao.class);
		//
		List<Map> result = dao.querytwo(resultId, id);
		// System.out.println(resultId);
		// System.out.println(id);
		// System.out.println(result);
		return result;
	}

	@RequestMapping(value = "/querydel.action", method = RequestMethod.POST)
	@ResponseBody
	public String querydel(
			@CookieValue(value = "result", defaultValue = "") String resultId,
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		SqlSession Session = MybatisSessionFactory.getSqlSession();
		LogDao dao1 = Session.getMapper(LogDao.class);
		UsersDao dao = Session.getMapper(UsersDao.class);
		dao.querydel(id);
		System.out.println(id);
		dao1.add2(resultId, "删除", id);
		Session.commit();
		return "OK";
	}
}